import {
  defineStore,
  type _ExtractActionsFromSetupStore,
  type _ExtractGettersFromSetupStore,
  type _ExtractStateFromSetupStore,
  type StoreDefinition,
} from 'pinia';
import { uniqueId } from 'lodash-es';

type StoreSetup = Parameters<typeof defineStore>[1];

/**
 * defineStore的包装，保证每次创建出来的store都是唯一的
 * @param setup
 * @returns
 */
function define<SS>(
  setup: () => SS,
  store_id = uniqueId('pinia-store#')
): StoreDefinition<
  string,
  _ExtractStateFromSetupStore<SS>,
  _ExtractGettersFromSetupStore<SS>,
  _ExtractActionsFromSetupStore<SS>
> {
  return defineStore<string, SS>(store_id, setup);
}

export { define };
export type { StoreSetup };
